import { computed } from 'vue';
export const usePlanAuthority = Variable => {
  const { modifyPlan,queryPlanList,searchFilter } = Variable;
  const hasAuth = (plan?) => {
    if (!plan) {
      plan = modifyPlan.value;
    }
    return !!(
      ((plan.PlanRange || (!plan.PlanRange && plan.IsAuth)) &&
        plan.PlanRange != 3) ||
      plan.WarningID
    );
  };
  const getSubStatus = (plan?) => {
    if (!plan) {
      plan = modifyPlan.value;
    }
    if (plan.IsAuth && plan.WarningID) {
      return 'cancel';
    } else if (plan.IsAuth && !plan.WarningID) {
      return 'subWarn';
    } else if (
      !plan.IsAuth &&
      (!plan.PlanRange || plan.PlanRange == 3) &&
      plan.WarningID
    ) {
      return 'viwSubWarn';
    } else if (
      !plan.IsAuth &&
      (plan.PlanRange == 1 || plan.PlanRange == 2) &&
      plan.WarningID
    ) {
      return 'cancel';
    }
  };
  const queryPlanByFilter = computed(() => {
    if (!(queryPlanList.value && queryPlanList.value.filter)) return [];
    if (searchFilter.value) {
      return queryPlanList.value.filter(item => {
        if (!item.Name) {
          return false;
        }
        if (item.Name.indexOf(searchFilter.value) != -1) {
          if (item.IsSystem && item.IsAuth) {
            return true;
          } else if (item.IsSystem) {
            return false;
          }
          return true;
        }
        return false;
      });
    }
    return queryPlanList.value.filter(item => {
      return item.Name ? true : false;
    });
  });
  return {
    hasAuth,
    getSubStatus,
    queryPlanByFilter
  };
};
